/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package XMLParserS;



import Server.FileParserI;
import Data.Car;
import Data.Employee;
import DataBase.DataFactory;
import DataBase.RequestI;
import DataBase.Requests;
import Server.ClientThread;
import java.io.IOException;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

// using SAX
public class ServerSaxParser implements FileParserI
{
    RequestI query;

    private Helper help = new Helper();
    private ClientThread clientTh = null;
    private Car car = null;
    private Employee employee = null;

    class ClientHandler extends DefaultHandler
    {



        boolean _request = false;
        boolean _db = false;
        boolean _db_password = false;

        boolean _check_user = false;
        boolean _user = false;
        boolean _password = false;

        boolean _insert_employee = false;
        boolean _firstName = false;
        boolean _lastName = false;
        boolean _earning = false;
        boolean _bonus = false;
        boolean _workHours = false;

        boolean _delete_reservation = false;
        boolean _dataRezervarii = false;

        boolean _make_reservation = false;
        boolean _plecare=false;
        boolean _sosire=false;
        boolean _pret=false;

        boolean _select_all = false;
        boolean _table = false;

        boolean _delete_car = false;
        boolean _carNumber = false;

        boolean _edit_route = false;
        boolean _dataCursei = false;

        boolean _route_report = false;

        boolean _month_report = false;
        boolean _startDate = false;
        boolean _endDate = false;

        boolean _select_car = false;
        boolean _edit_car = false;
        boolean _insert_car = false;
        boolean _carId = false;
        boolean _carModel = false;
        boolean _kmNumber = false;
        boolean _seatsNumber = false;
        boolean _maintenanceCost = false;
        boolean _fuelConsumption = false;
        boolean _car = false;

        boolean _cities_list = false;
        boolean _get_km = false;
        boolean _departure = false;
        boolean _arrival = false;

        boolean _totalCost= false;
        boolean _employeeSalary = false;

         boolean _new_user = false;
        boolean _delete_user = false;

        boolean _edit_employees = false;
        boolean _employee = false;

        boolean _deleteEmployee = false;



        @Override
        public void startElement(String nsURI,
                                 String strippedName,
                                 String tagName,
                                 Attributes attributes)  throws SAXException
        {

            if (tagName.equalsIgnoreCase("request"))
                 _request = true;

            if (tagName.equalsIgnoreCase("db"))
                 _db = true;
            if (tagName.equalsIgnoreCase("db_password"))
                 _db_password = true;


            if (tagName.equalsIgnoreCase("check_user"))
                _check_user = true;
            if (tagName.equalsIgnoreCase("user"))
                _user = true;
            if (tagName.equalsIgnoreCase("password"))
                 _password = true;

             if (tagName.equalsIgnoreCase("insert_employee"))
                _insert_employee = true;
            if (tagName.equalsIgnoreCase("firstName"))
                _firstName = true;
            if (tagName.equalsIgnoreCase("lastName"))
               _lastName = true;
            if (tagName.equalsIgnoreCase("earning"))
                _earning = true;
            if (tagName.equalsIgnoreCase("bonus"))
                _bonus = true;
            if (tagName.equalsIgnoreCase("workHours"))
                _workHours = true;

            if (tagName.equalsIgnoreCase("delete_reservation"))
                _delete_reservation = true;
              if (tagName.equalsIgnoreCase("dataRezervarii"))
                _dataRezervarii = true;

             if (tagName.equalsIgnoreCase("make_reservation"))
                _make_reservation = true;
             if (tagName.equalsIgnoreCase("plecare"))
             {
                 _plecare = true;
             }

             if (tagName.equalsIgnoreCase("sosire"))
                _sosire = true;
             if (tagName.equalsIgnoreCase("pret"))
                _pret = true;

            if (tagName.equalsIgnoreCase("select_all"))
                _select_all = true;
            if (tagName.equalsIgnoreCase("table"))
                _table = true;

            if (tagName.equalsIgnoreCase("delete_car"))
                _delete_car = true;
            if (tagName.equalsIgnoreCase("carNumber"))
                _carNumber = true;

            if (tagName.equalsIgnoreCase("select_car"))
                _select_car = true;
            if (tagName.equalsIgnoreCase("insert_car"))
                _insert_car = true;
            if (tagName.equalsIgnoreCase("edit_car"))
                _edit_car = true;
             if (tagName.equalsIgnoreCase("carId"))
                _carId = true;
            if (tagName.equalsIgnoreCase("carModel"))
                _carModel = true;
            if (tagName.equalsIgnoreCase("kmNumber"))
                _kmNumber = true;
            if (tagName.equalsIgnoreCase("seatsNumber"))
                _seatsNumber = true;
            if (tagName.equalsIgnoreCase("maintenanceCost"))
                _maintenanceCost = true;
            if (tagName.equalsIgnoreCase("fuelConsumption"))
                _fuelConsumption = true;
             if (tagName.equalsIgnoreCase("car"))
                _car = true;


             if (tagName.equalsIgnoreCase("edit_route"))
                _edit_route = true;
             if (tagName.equalsIgnoreCase("dataCursei"))
                _dataCursei = true;

              if (tagName.equalsIgnoreCase("route_report"))
                _route_report = true;

             if (tagName.equalsIgnoreCase("month_report"))
                _month_report = true;
             if (tagName.equalsIgnoreCase("startDate"))
                _startDate = true;
             if (tagName.equalsIgnoreCase("endDate"))
                _endDate = true;

             if (tagName.equalsIgnoreCase("cities_list"))
                _cities_list = true;

            //gt km
             if (tagName.equalsIgnoreCase("get_km"))
                _get_km = true;

             if (tagName.equalsIgnoreCase("departure"))
                _departure = true;

             if (tagName.equalsIgnoreCase("arrival"))
                _arrival = true;

            //total cost
             if (tagName.equalsIgnoreCase("total_cost"))
                _totalCost = true;

             if (tagName.equalsIgnoreCase("employee_salary"))
                _employeeSalary = true;

                 if (tagName.equalsIgnoreCase("new_user"))
                _new_user = true;

             if (tagName.equalsIgnoreCase("delete_user"))
                _delete_user = true;
               if (tagName.equalsIgnoreCase("edit_employees"))
                _edit_employees = true;

             if (tagName.equalsIgnoreCase("employee"))
                _employee = true;
              if (tagName.equalsIgnoreCase("delete_employee")){

                  _deleteEmployee = true;
                  System.out.println("***************** _deleteEmployee=true;");
              }
                


        }



        @Override
        public void characters(char[] ch, int start, int length)
        {


           if (_user && _check_user)
           {
                help.Clear();
                help.setUser(new String(ch, start, length));
               _user = false;
           }
            else if(_password && _check_user){

                help.setPassword(new String(ch, start, length));
               _password = false;

           }
           else if (_db && _check_user)
           {

                help.db = new String(ch, start,length);
               _db = false;

           }
           else if (_db_password && _check_user)
           {
                help.setDb_password(new String(ch, start, length));
                _db_password = false;
           }
      // insert_employee

           else if ((_insert_employee || _new_user || _delete_user) && _firstName)
           {
               help.setFirstName(new String(ch, start,length));
               _firstName = false;
           }
           else if ((_insert_employee || _new_user || _delete_user) && _lastName)
           {
               help.setLastName(new String(ch, start,length));
               _lastName = false;
           }
            else if (_insert_employee && _earning)
           {
               help.setEarning(Integer.parseInt(new String(ch, start,length)));
               _earning = false;
           }else if (_insert_employee && _bonus)
           {
               help.setBonus(Integer.parseInt(new String(ch, start,length)));
               _bonus = false;
           }else if (_insert_employee && _workHours)
           {
               help.setWorkHours(Integer.parseInt(new String(ch, start,length)));
               _workHours = false;
               System.out.println("sax parser work hour "+help.getWorkHours());
           }

           //delete reservation
           else if(_delete_reservation && _firstName){
               help.setFirstName(new String(ch, start,length));
               _firstName=false;
           }
           else if (_delete_reservation && _lastName)
           {
               help.setLastName(new String(ch, start,length));
               _lastName = false;
           }
            else if (_delete_reservation && _dataRezervarii)
           {

                    String x = new String(ch, start, length);

                     System.out.println("Date in server sax parser: "+x);
                    help.setDataRezervarii(x);
                    _dataRezervarii = false;

           }

              //make reservation
            else if(_make_reservation && _firstName){
               help.setFirstName(new String(ch, start,length));
               _firstName=false;
           }
           else if (_make_reservation && _lastName)
           {
               help.setLastName(new String(ch, start,length));
               _lastName = false;
           }
            else if (_make_reservation && _dataRezervarii)
           {

                    String x = new String(ch, start, length);

                     System.out.println("Date in server sax parser: "+x);
                    help.setDataRezervarii(x);
                    help.getTicket().setDepartureDate(x);
                     help.getTicket().setArrivalDate(x);
                    _dataRezervarii = false;

           }
            else if(_make_reservation && _pret){
                String x = new String(ch,start,length);
                help.getTicket().setPrice(Double.parseDouble(x));
                _pret=false;
            }
           else if(_make_reservation && _plecare){
                String x = new String(ch,start,length);
                System.out.println("******************************ASTA E ORASUL PE CRAE TRE SA IL PUNA  "+x);
                help.getTicket().setDeparture(x);
                _plecare=false;
           }
           else if(_make_reservation && _sosire){
                String x = new String(ch,start,length);
               help.getTicket().setDestination(x);
               _sosire=false;
           }

            // select all
            else if (_select_all && _table)
           {
               help.setTable(new String(ch, start,length));
               _table = false;
           }
           //delete car
            else if (_delete_car && _carNumber)
           {
               help.setNumber(new String(ch, start,length));
               _carNumber = false;
           }
    else if (_select_car && _carNumber)
           {
               help.setNumber((new String(ch, start,length)));
               _carNumber = false;
           }
             else if (_edit_car && _carId)
           {
               car.setCarId(Integer.parseInt(new String(ch, start,length)));
               _carId = false;
           }

            else if((_edit_car || _insert_car) && _car)
              {
                 car = new Car();
                 _car = false;
                  System.out.println("new car ******");
              }
              else if(_edit_car  && _carId )
              {
                car.setCarId(Integer.parseInt(new String(ch, start,length)));
                _carId = false;
                System.out.println("id "+Integer.parseInt(new String(ch, start,length)));
              }
              else if((_edit_car || _insert_car) && _carNumber )
              {
                car.setNumber(new String(ch, start,length));
                _carNumber = false;
                 System.out.println("carnumber "+(new String(ch, start,length)));
              }
              else if((_edit_car || _insert_car) && _carModel )
              {
                car.setModel(new String(ch, start,length));
                _carModel = false;
                 System.out.println("model "+(new String(ch, start,length)));
              }
               else if((_edit_car || _insert_car) && _kmNumber )
              {
                car.setKmNumber(Integer.parseInt(new String(ch, start,length)));
                _kmNumber = false;
                 System.out.println("km "+Integer.parseInt(new String(ch, start,length)));
              }
              else if((_edit_car || _insert_car) && _seatsNumber )
              {
                car.setSeatsNumber(Integer.parseInt(new String(ch, start,length)));
                _seatsNumber = false;
                System.out.println("seats:"+car.getSeatsNumber());
              }
               else if((_edit_car || _insert_car) && _fuelConsumption )
              {
                car.setFuelConsumption(Double.parseDouble(new String(ch, start,length)));
                _fuelConsumption = false;
                 System.out.println("fuel "+Double.parseDouble(new String(ch, start,length)));
              }
              else if((_edit_car || _insert_car) && _maintenanceCost )
              {
                car.setMaintenanceCost(Double.parseDouble(new String(ch, start,length)));
                _maintenanceCost = false;
                 System.out.println("maint "+Double.parseDouble(new String(ch, start,length)));
              }



           //edit route - adaug sofer si masina pt cursa de la o anumita data
            else if(_edit_route && _firstName){
               help.setFirstName(new String(ch, start,length));
               _firstName=false;
           }
           else if (_edit_route && _lastName)
           {
               help.setLastName(new String(ch, start,length));
               _lastName = false;
           }
            else if (_edit_route && _dataCursei)
           {
                    help.setDataRezervarii(new String(ch, start, length));
                    _dataCursei = false;
           }
           else if (_edit_route && _carNumber)
           {
                    help.setNumber(new String(ch, start, length));
                    System.out.println("*******************************");
                    System.out.println("in server sax parseer cand setez in help numarul"+help.getNumber());
                    _carNumber = false;
           }

           //route report - toate bilete de la o anumita cursa
           else if(_route_report && _dataCursei){
                help.setDataRezervarii(new String(ch, start, length));
                _dataCursei = false;
           }

           //month report = toate biletele dintr-o luna
            else if(_month_report && _startDate){
                help.setStartDate(new String(ch, start, length));
                _startDate = false;
           }
            else if(_month_report && _endDate){
                help.setEndDate(new String(ch, start, length));
                _endDate = false;
           }

            else if(_get_km && _departure){
                help.setDeparture(new String(ch, start, length));
                _departure = false;
           }
            else if(_get_km && _arrival){
                help.setArrival(new String(ch, start, length));
                _arrival = false;
           }


            else if(_cities_list)
            {
               query.getCitiesList(clientTh, help.db);
                _cities_list = false;
            }
             else if(( _new_user || _delete_user)&& _kmNumber)
            {
                help.setKmNumber(Integer.parseInt(new String(ch,start,length)));
                _kmNumber = false;

            }
           // edit employees
           else if(_edit_employees && _firstName && _employee){
                    employee = new Employee();
                    employee.setFirstName(new String(ch,start,length));
                    _firstName = false;

                 }
                else if(_edit_employees && _lastName && _employee){
                    employee.setLastName(new String(ch,start,length));
                    _lastName=false;

                 }
              else if(_edit_employees && _earning && _employee){
                    employee.setEarning(Integer.parseInt(new String(ch,start,length)));
                    _earning=false;
                 }
             else if(_edit_employees && _workHours && _employee){
                    employee.setWorkHours(Integer.parseInt(new String(ch,start,length)));
                    _workHours=false;
                    System.out.println("work h"+employee.getWorkHours());
                 }
             else if(_edit_employees && _bonus && _employee){
                    employee.setBonus(Integer.parseInt(new String(ch,start,length)));
                    _bonus=false;

                 }
            else if(_deleteEmployee && _firstName){
                    employee = new Employee();
                    employee.setFirstName(new String(ch,start,length));
                    System.out.println("******In SERVER SAX PARSER******* "+employee.getFirstName());

                    _firstName = false;

                 }
                else if(_deleteEmployee && _lastName){
                    employee.setLastName(new String(ch,start,length));
                    _lastName=false;

                 }
}
       @Override
        public void endElement(String uri, String localName, String tagName) throws SAXException {

           if (tagName.equalsIgnoreCase("check_user"))
           {
               System.out.println("endElement - check_user" );
                if(help.getDb() != null && help.getDb_password() != null)
                {
                  // query = new Requests(help.getDb(), help.getDb_password());
                    DataFactory factory = DataFactory.getInstance();
                    query = factory.makeRequest(help.getDb(), help.getDb_password());

                   if(help.getUser() != null && help.getPassword() != null)
                   query.CheckUserPassword(clientTh, help.getDb(), help.getUser(), help.getPassword());
                }
           }

           if (tagName.equalsIgnoreCase("insert_employee"))
           {
               _insert_employee = false;
               query.insertEmployee(clientTh, help);
           }

           if (tagName.equalsIgnoreCase("delete_reservation"))
           {
               _delete_reservation = false;
               System.out.println("am ajund aici in server sax parser inainte sa aplelez delete reservation");
               query.deleteReservation(clientTh, help);
               System.out.println("am apelat delete reservation");
           }


           if (tagName.equalsIgnoreCase("make_reservation"))
           {
               _make_reservation = false;
               System.out.println("am ajund aici in server sax parser inainte sa aplelez make reservation");
               query.makeReservation(clientTh, help);
               System.out.println("am apelat make reservation");
           }

            if (tagName.equalsIgnoreCase("select_all"))
           {
               _select_all = false;
               query.selectAll(clientTh, help.getDb(), help.getTable());

           }

            if (tagName.equalsIgnoreCase("edit_route"))
           {
               _edit_route = false;
               query.editRoute(clientTh, help.getFirstName(), help.getLastName(), help.getNumber(), help.getDataRezervarii());

           }
            if (tagName.equalsIgnoreCase("delete_car"))
           {
               _select_all = false;
               query.deleteCar(clientTh, help.getNumber());
           }
             if (tagName.equalsIgnoreCase("route_report"))
           {
               _route_report = false;
               query.routeReport(clientTh, help.getDataRezervarii());

           }
            if (tagName.equalsIgnoreCase("select_car"))
           {
               _select_car = false;
               query.selectCar(clientTh, help.getDb(), help.getNumber());
           }
           if (tagName.equalsIgnoreCase("edit_car"))
           {
               _select_car = false;
               query.editCar(clientTh, help.getDb(), car);
           }
           if (tagName.equalsIgnoreCase("insert_car"))
           {
               _select_car = false;
               query.insertCar(clientTh, help.getDb(), car);
           }
            if (tagName.equalsIgnoreCase("month_report"))
           {
               System.out.println("****** INAINTE SA APELEZ MONTH REPORT(sunt in server sax parser)");
               _month_report = false;
               query.monthReport(clientTh, help.getStartDate(), help.getEndDate());

           }
           if (tagName.equalsIgnoreCase("get_km"))
           {
               _get_km = false;
               query.getKm(clientTh, help.getDb(),
                       help.getDeparture(), help.getArrival());
           }

           if (tagName.equalsIgnoreCase("total_cost"))
           {
               _totalCost = false;
               query.totalMaintananceCost(clientTh);
           }
            if (tagName.equalsIgnoreCase("employee_salary"))
           {
               _employeeSalary = false;
               query.employeeSalary(clientTh);
           }
            if (tagName.equalsIgnoreCase("new_user"))
           {
               _new_user = false;
               query.newUser(clientTh, help.getDb(),
                       help.getFirstName(), help.getLastName(), help.getKmNumber());
           }
           if(tagName.equalsIgnoreCase("delete_user"))
           {
                _delete_user = false;
                 query.deleteUser(clientTh, help.getDb(),help.getFirstName(), help.getLastName());
           }
             if (tagName.equalsIgnoreCase("employee") && _edit_employees)
           {
               _employee = false;
               System.out.println("************EDIT employee in SERVER SAX PARSER"+employee.toString());
               query.editEmployee(clientTh, employee);

               System.out.println(employee.toString());
               employee = null;
           }
           if(tagName.equalsIgnoreCase("delete_employee"))
           {
               _deleteEmployee = false;
               query.deleteEmployee(clientTh, employee.getFirstName(),
                                        employee.getLastName());
               System.out.println("************delete employee in SERVER SAX PARSER"+employee.toString());
               employee = null;
           }

           help.Clear();
           _request = false;

        }
    }
    public void list(ClientThread ser, String fileName)  {
        try {
            clientTh = ser;
            XMLReader parser = XMLReaderFactory.createXMLReader();
            //("org.apache.crimson.parser.XMLReaderImpl");
            parser.setContentHandler(new ClientHandler());
            parser.parse(fileName);
        } catch (IOException ex) {
            Logger.getLogger(ServerSaxParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(ServerSaxParser.class.getName()).log(Level.SEVERE, null, ex);
        }
       }


    public static void main(String[] args) throws Exception {
       ServerSaxParser p = new ServerSaxParser();
     //  p.list("S2C.xml");

       }
}










